package com.ruoyi.web.pojo.request.insect;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.net.url.UrlBuilder;
import com.ruoyi.common.utils.CommonUtils;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class InsectHistoryDataRequest {

    @ApiModelProperty(value = "设备ID")
    @NotEmpty(message = "设备id不能为空")
    private String deviceId;

    @ApiModelProperty(value = "数据开始时间,格式 yyyy-MM-dd")
//    private String startTime = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH,-30).toString("yyyy-MM-dd");
    private String startTime;

    @ApiModelProperty(value = "数据结束时间")
    private String endTime;

    @ApiModelProperty(value = "日期类型，1日报表") //，2月报表，3年报表
    private Integer dateType = 1;

    @ApiModelProperty(value = "设备ID集合")
    private List<String> deviceIds;

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
        this.deviceIds = CollUtil.newArrayList(deviceId);
    }

    public void setEndTime(String endTime) {
        if(endTime != null){
            this.endTime = CommonUtils.computeTime(endTime,1);
        }
    }

    public void buildDefTime(String endTime) {
        this.setEndTime(endTime);
        this.startTime = CommonUtils.computeTime(endTime,-15);
    }

    /**
     * @desc  得到本月的查询日期
     * @author fwx 2024/2/19 14:27
     **/
    public static InsectHistoryDataRequest getMonth(List<String> deviceIds){
        InsectHistoryDataRequest request = new InsectHistoryDataRequest();
        request.setStartTime(DateUtils.getMonthBeginDate());
        request.setEndTime(DateUtils.getTomorrowStr());
        request.setDeviceIds(deviceIds);
        return request;
    }


    /**
     * @desc  得到本年的查询日期
     * @author fwx 2024/2/19 14:27
     **/
    public static InsectHistoryDataRequest getYear(List<String> deviceIds){
        InsectHistoryDataRequest request = new InsectHistoryDataRequest();
        request.setStartTime(DateUtils.getYearBeginDate());
        request.setEndTime(DateUtils.getTomorrowStr());
        request.setDeviceIds(deviceIds);
        return request;
    }

    /**
     * @desc  得到昨日的查询日期
     * @author fwx 2024/2/19 14:39
     **/
    public static InsectHistoryDataRequest getDay(List<String> deviceIds){
        InsectHistoryDataRequest request = new InsectHistoryDataRequest();
        request.setStartTime(DateUtils.getMonthBeginDate());
        request.setEndTime(DateUtils.getTomorrowStr());
        request.setDeviceIds(deviceIds);
        return request;
    }

    public UrlBuilder getQuery(){
        UrlBuilder builder = UrlBuilder.of();
        if (CollUtil.isNotEmpty(deviceIds)){
            builder.addQuery("deviceId",this.getDeviceIds().stream().collect(Collectors.joining(",")));
        }
        builder.addQuery("dateType",this.getDateType());
        if (this.getStartTime() != null) builder.addQuery("startTime",this.getStartTime());
        if (this.getEndTime() != null) builder.addQuery("endTime",this.getEndTime());

        return builder;
    }
}
